<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_contract;

use Plugin\Attractions\Commerce\Model\Commerce_contract\CommerceContract;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 合同 Repository类
 */
class CommerceContractRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceContract $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 合同编号
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 商户ID
        if (isset($params['merchant_id']) && !empty($params['merchant_id'])) {
            $query->where('merchant_id', '=', $params['merchant_id']);
        }

        // 合同标题
        if (isset($params['contract_title']) && !empty($params['contract_title'])) {
            $query->where('contract_title', 'like', '%'.$params['contract_title'].'%');
        }

        // 甲方
        if (isset($params['party_a']) && !empty($params['party_a'])) {
            $query->where('party_a', 'like', '%'.$params['party_a'].'%');
        }

        // 乙方
        if (isset($params['party_b']) && !empty($params['party_b'])) {
            $query->where('party_b', 'like', '%'.$params['party_b'].'%');
        }

        // 合同金额
        if (isset($params['contract_amount']) && !empty($params['contract_amount'])) {
            $query->where('contract_amount', '=', $params['contract_amount']);
        }

        // 签订日期
        if (isset($params['sign_date']) && !empty($params['sign_date']) && is_array($params['sign_date']) && count($params['sign_date']) == 2) {
            $query->whereBetween(
                'sign_date',
                [ $params['sign_date'][0], $params['sign_date'][1] ]
            );
        }

        // 生效日期
        if (isset($params['effective_date']) && !empty($params['effective_date']) && is_array($params['effective_date']) && count($params['effective_date']) == 2) {
            $query->whereBetween(
                'effective_date',
                [ $params['effective_date'][0], $params['effective_date'][1] ]
            );
        }

        // 到期日期
        if (isset($params['expiry_date']) && !empty($params['expiry_date']) && is_array($params['expiry_date']) && count($params['expiry_date']) == 2) {
            $query->whereBetween(
                'expiry_date',
                [ $params['expiry_date'][0], $params['expiry_date'][1] ]
            );
        }

        // 合同文件路径
        if (isset($params['contract_file']) && !empty($params['contract_file'])) {
            $query->where('contract_file', '=', $params['contract_file']);
        }

        // 附件文件路径（JSON格式）
        if (isset($params['attachment_files']) && !empty($params['attachment_files'])) {
            $query->where('attachment_files', '=', $params['attachment_files']);
        }

        // 付款方式1-银行转账,2-微信支付,3-支付宝支付,4-现金,5-其他付款方式
        if (isset($params['payment_method']) && !empty($params['payment_method'])) {
            $query->where('payment_method', '=', $params['payment_method']);
        }

        // 合同备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', '=', $params['remark']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}